package test.yzx.pproject.act4;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import test.yzx.pproject.R;

public class DialogActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onBackPressed() {
        showDialog();
    }


    private DialogView dialog;
    private void showDialog(){
        if(dialog!=null){
            ViewCompat.animate(dialog.getWrapperView()).alpha(0f).setDuration(500).start();
            ViewCompat.animate(dialog.getContentView()).scaleX(0f).scaleY(0f).setDuration(500).setListener(new ViewPropertyAnimatorListenerAdapter(){
                public void onAnimationEnd(View view) {
                    dialog.remove();
                    dialog = null;
                }}).start();
        }else{
            dialog = new DialogView(this);
            dialog.show(View.inflate(this, R.layout.layout_dialog_test, null));
            dialog.getWrapperView().setBackgroundColor(Color.parseColor("#66000000"));
            dialog.getWrapperView().setAlpha(0f);
            ViewCompat.animate(dialog.getWrapperView()).alpha(1f).setDuration(500).start();
            dialog.getContentView().post(new Runnable() {public void run() {
                dialog.getContentView().setScaleX(0f);
                dialog.getContentView().setScaleY(0f);
                ViewCompat.animate(dialog.getContentView()).scaleX(1f).scaleY(1f).setDuration(500).start();
            }});
        }
    }



}


